<template>

	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="店铺查询"></el-input>
				</el-form-item>
				<el-form-item>
          状态:
          <el-select v-model="value" placeholder="请选择">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
           &ensp;
					<el-button type="primary" v-on:click="getempl">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>

			</el-form>
		</el-col>

		<!--列表-->
<!--		:data :绑定的数据-->
<!--		v-loading: 页面css样式等东西加载之后加载加载数据-->
<!--		prop:对应的是:data绑定的对象的属性-->
<!--		label :描述-->
		<el-table :data="audit" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="店铺名" width="120" sortable>
			</el-table-column>

      <el-table-column  label="店铺Logo" width="100" >
          <template slot-scope="scope">
            <el-image
                style="width: 100px; height: 100px"
                :src="scope.row.logoStr"
                :fit="fit">

            </el-image>
          </template>
      </el-table-column>

			<el-table-column prop="state" label="状态" width="100" :formatter="formatSex" sortable>
			</el-table-column>

			<el-table-column label="操作" width="500">
				<template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">详情</el-button>
					<el-button type="success" size="small" @click="handleEdit(scope.$index, scope.row)">通过</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">拒绝</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--分页条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
<!--			current-change:自带的事件,我当前页发生改变时,就会触发此事件-->
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="5" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="员工姓名" prop="username">
					<el-input v-model="editForm.username" auto-complete="off"></el-input>
				</el-form-item>

        <el-form-item label="员工邮箱" prop="email">
          <el-input v-model="editForm.email" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input v-model="editForm.phone" auto-complete="off"></el-input>
        </el-form-item>



        <el-form-item label="上级领导" prop="parent">
          <!--	{
          value: 'zhinan',指定选项的值为选项对象的某个属性值
          label: '指南',指定选项标签为选项对象的某个属性值
          children: []
          options:{id:1,"name":zs,children: []}
          	}-->
          <el-cascader v-model="editForm.parentId"
                       :options="emplTree"
                       :props="{ checkStrictly: true ,value:'id',label:'username'}"
                       clearable></el-cascader>
        </el-form-item>



      </el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
    <!--列表-->
    <!--		:data :绑定的数据-->
    <!--		v-loading: 页面css样式等东西加载之后加载加载数据-->
    <!--		prop:对应的是:data绑定的对象的属性-->
    <!--		label :描述-->
		<el-dialog title="新增" :visible.sync="addFormVisible"  :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="员工姓名" prop="username">
					<el-input v-model="addForm.username" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="员工邮箱" prop="email">
          <el-input v-model="addForm.email" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input v-model="addForm.phone" auto-complete="off"></el-input>
        </el-form-item>



        <el-form-item label="上级领导" prop="parent">
          <!--	{
          value: 'zhinan',指定选项的值为选项对象的某个属性值
          label: '指南',指定选项标签为选项对象的某个属性值
          children: []
          options:{id:1,"name":zs,children: []}
          	}-->
          <el-cascader v-model="addForm.parentId"
              :options="emplTree"
              :props="{ checkStrictly: true ,value:'id',label:'username'}"
              clearable></el-cascader>
        </el-form-item>



      </el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>

	//import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	//import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
        fits: [''],
        imageUrl: '',
				filters: {
					name: ''
				},
        //这里获取的应该是所有部门的一个部门树
        emplTree:[],
        employees:[],
				audit: [],
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
				editForm: {
					id: '',
					name: '',
				  sn:'',
          manager:[],
          parentId:""
        },

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
					username: '',
					email:'',
          phone:[],
          parentId:""

				},
        options: [{
          value: '',
          label: '全部'
        }, {
          value: '1',
          label: '通过'
        }, {
          value: '0',
          label: '拒绝'
        }],
        value: ''

			}
		},
		methods: {
			//性别显示转换
			//row 当前行row = user  column当前列
      handleAvatarSuccess(res, file) {//res就是上传成功后，后台返回的结果
        console.log(JSON.stringify(res));
       // this.imageUrl = res.data.logo;
      },
			formatSex: function (row, column) {
				return row.state == 1 ? '通过' : row.state == 0 ? '拒绝' : '未知';
			},
			handleCurrentChange(val) {
				//var 就是当前页的数据
				this.page = val;
				this.getempl();
			},
			//获取部门列表
			getempl() {
				//封装的数据
				let para = {
					//page 就是当前页
					page: this.page,
					//这个是高级查询的条件
					keyword: this.filters.name,
          //这个是店铺状态
          state:this.value

				};
				this.listLoading = true;
				//NProgress.start();
				this.$http.get("/shop/findAll").then((res) => {
					// console.log("后端发送过来的分页数据:"+JSON.stringify(res));
					//
					//this.total = res.data.total;
					//
					this.audit = res.data;
          console.log(this.audit)
					//关闭进度条
					this.listLoading = false;
					//NProgress.done();
				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { id: row.id };
				this.$http.post("empl/delete",para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getempl();
					});
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				//Object.assign 是拷贝对象 BeanUtil.copyof()
				this.editForm = Object.assign({}, row);
        //console.log(JSON.stringify(row))
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
					name: '',
				  sn:''
				};
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
              para.parentId =  para.parentId[para.parentId.length-1]
							this.$http.post("/empl/update",para).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getempl();
							});
						});
					}
				});
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							// Object.assign 就是用来复制对象的
							let para = Object.assign({}, this.addForm);
              para.parentId =  para.parentId[para.parentId.length-1]
							this.$http.post("/empl/add",para).then((res) => {
								this.addLoading = false;
								////elementUi的提示弹框
								this.$message({
									message: '提交成功',
									type: 'success'
								});
                //重新set数据
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getempl();
							});
						});
					}
				});
			},
			//复选勾选框的方法
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
        //map 就是java的stream流 list.stream.map
        //sels = 当前勾选的对象的集合
        //通过map方法获取到每个对象item
        //ids = [1,2,3,4]
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
          //这里开始处理数据,封装数据
					this.listLoading = true;
					//NProgress.start();
          //将ids 数组封装成json 字符串 此处不需要它封装了 直接传数组
					//let para = { ids: ids };
					this.$http.post("/empl/BatchDelete",ids).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getempl();
					});
				}).catch(() => {

				});
			},
      //获取员工列表
      getEmployee(){
      this.$http.get("/empl/findEmpls").then(res=>{
        this.employees = res.data
      }).catch(res=>{

      })
      },
      getEmplTree(){
        this.$http.get("/empl/tree").then(res=>{
          this.emplTree = res.data
        }).catch(res=>{

        })
      }
		},
		mounted() {
			this.getempl();
      this.getEmployee();
      this.getEmplTree();
		}
	}

</script>

<style scoped>

</style>
